package win.ganbo.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import win.ganbo.ssm.dao.UserMapper;
import win.ganbo.ssm.entity.User;
import win.ganbo.ssm.event.UserEventDemoTest;
import win.ganbo.ssm.service.IUserService;

import java.util.List;
import java.util.concurrent.Future;

/**
 *
 * @Author ganbo E-mail:bo.gan@56qq.com
 * @CreateTime 2017-08-01 20:56
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Transactional(rollbackFor = Exception.class)
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    public void save(User user) {
        user.setUsername("==================");
        int insert = userMapper.insert(user);
        System.out.println("=count===========>" + insert);
        System.out.println("==========开始保存===");
        save2222222(user);
        System.out.println("保存成功=====");
    }

    @Transactional(rollbackFor = Exception.class)
    public void save2222222(User user) {
        userMapper.insert(user);
        int b = 0;
        int a = 10 / b;
    }

    public PageInfo<User> queryByPage(String userName, Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        List<User> list = userMapper.selectUserByUserName(userName);
        //用PageInfo对结果进行包装j
        PageInfo<User> page = new PageInfo<User>(list);
        //测试PageInfo全部属性
        System.out.println(page.getPageNum());
        System.out.println(page.getPageSize());
        System.out.println(page.getStartRow());
        System.out.println(page.getEndRow());
        System.out.println(page.getTotal());
        System.out.println(page.getPages());
        System.out.println(page.getFirstPage());
        System.out.println(page.getLastPage());
        System.out.println(page.isHasPreviousPage());
        System.out.println(page.isHasNextPage());

        return page;
    }

    @Async
    public Future<String> asyncTest() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1 * 1000);
                System.out.println("倒计时===>" + (1 + i) + "s");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

        System.out.println("========================异步调用测试...");

        return new AsyncResult<String>("Async返回结果了,休息了===>" + 5 + "秒");
    }

}
